<template>
  <div class="divBox relative">
    <el-card class="box-card">
      <div class="clearfix">
        <div class="container">
          <el-form size="small" inline label-width="100px">
            <el-form-item label="订单号：">
              <el-input
                v-model="tableFrom.outTradeNo"
                placeholder="请输入订单号"
                style="width: 200px"
                size="small"
                clearable
              />
            </el-form-item>
            <el-form-item label="购买人：">
              <el-input
                v-model="tableFrom.buyPhone"
                placeholder="请输入购买人"
                style="width: 200px"
                size="small"
                clearable
              />
            </el-form-item>
            <el-form-item label="批发商：">
              <el-input
                v-model="tableFrom.sellPhone"
                placeholder="请输入批发商"
                style="width: 200px"
                size="small"
                clearable
              />
            </el-form-item>
            <el-form-item label="交易状态：">
              <el-select
                v-model="tableFrom.status"
                style="width: 200px"
                placeholder="请选择交易状态"
              >
                <el-option label="未支付" :value="0"> </el-option>
                <el-option label="已支付" :value="1"> </el-option>
                <el-option label="完成" :value="2"> </el-option>
                <el-option label="争议" :value="3"> </el-option>
                <el-option label="争议完成" :value="4"> </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="商品名称：">
              <el-input
                v-model="tableFrom.storeName"
                placeholder="请输入商品名称"
                style="width: 200px"
                size="small"
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                @click="seachList"
                size="small"
                v-hasPermi="['admin:product:list']"
              >
                搜索
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-card>
    <div class="mt20">
      <!-- <cards-data :cardLists="cardLists" v-if="checkPermi(['admin:order:list:data'])"></cards-data> -->
    </div>
    <el-card class="box-card">
      <el-table
        v-loading="listLoading"
        :data="tableData.data"
        size="mini"
        class="table"
        highlight-current-row
        :header-cell-style="{ fontWeight: 'bold' }"
        :row-key="
          (row) => {
            return row.orderId;
          }
        "
      >
        <el-table-column label="订单号" min-width="180">
          <template slot-scope="scope">
            <span style="display: block" v-text="scope.row.outTradeNo" />
            <span
              v-show="scope.row.isDel"
              style="color: #ed4014; display: block"
              >用户已删除</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="storeName" label="商品名称" min-width="260" />
        <el-table-column prop="proTotalPrice" label="批发价格" min-width="80" />
        <el-table-column prop="otPrice" label="市场价格" min-width="80" />
        <el-table-column prop="buyPhone" label="购买人" min-width="100" />
        <el-table-column prop="sellPhone" label="批发商" min-width="100" />
        <el-table-column label="交易状态" min-width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.paid">已支付</span>
            <span v-else>未支付</span>
          </template>
        </el-table-column>
        <el-table-column label="是否提货" min-width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.isTakeGoods == 0">提货申请</span>
            <span v-if="scope.row.isTakeGoods == 1">提货完成</span>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" min-width="80"
          ><template slot-scope="scope">
            <span v-if="scope.row.status == 0">待支付</span>
            <span v-if="scope.row.status == 1">已支付</span>
            <span v-if="scope.row.status == 2">已完成</span>
            <span v-if="scope.row.status == 3">争议</span>
            <span v-if="scope.row.status == 4">争议完成</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="订单时间" min-width="150" />
        <el-table-column prop="payTime" label="付款时间" min-width="150" />
        <!-- <el-table-column
          prop="createTime"
          label="确认收款时间"
          min-width="150"
        /> -->

        <el-table-column
          label="操作"
          min-width="100"
          fixed="right"
          align="center"
        >
          <template slot-scope="scope">
            <!-- <el-button
              type="text"
              size="small"
              @click="edit(scope.row)"
              class="mr10"
              v-hasPermi="['admin:order:update:price']"
              >取消订单</el-button
            > -->
            <el-button
              type="text"
              size="small"
              class="mr10"
              @click="sendOrder(scope.row)"
              v-hasPermi="['admin:order:send']"
              >争议处理</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          :page-sizes="[20, 40, 60, 80]"
          :page-size="tableFrom.limit"
          :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total"
          @size-change="handleSizeChange"
          @current-change="pageChange"
        />
      </div>
    </el-card>

    <!--争议处理-->
    <el-dialog
      title="争议处理"
      v-if="zyVisible"
      :visible.sync="zyVisible"
      width="500px"
      :before-close="zyClose"
    >
      <el-form ref="form" :model="zyform" label-width="100px">
        <el-form-item label="状态">
          <el-select v-model="zyform.status" placeholder="请选择状态">
            <el-option label="已完成" value="2"></el-option>
            <el-option label="判断完成" value="4"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="解封用户状态">
          <el-select v-model="zyform.type" placeholder="请选择用户状态">
            <el-option label="所有" value="0"></el-option>
            <el-option label="买家" value="1"></el-option>
            <el-option label="卖家" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input
            v-model="zyform.remark"
            placeholder="请输入备注"
            type="textarea"
            maxlength="250"
            :rows="3"
            style="width: 200px"
            size="small"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="sumbitZyVisible"
            size="small"
            v-hasPermi="['admin:product:list']"
          >
            确定
          </el-button>
          <el-button @click="zyVisible = false" size="small"> 取消 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  orderListDataApi,
  orderStatusNumApi,
  writeUpdateApi,
  orderListApi,
  biddingOrderListApi,
  updatePriceApi,
  orderLogApi,
  orderMarkApi,
  orderDeleteApi,
  orderRefuseApi,
  orderRefundApi,
  orderPrint,
  biddingOrderDispute,
} from "@/api/order";
import cardsData from "@/components/cards/index";
import zbParser from "@/components/FormGenerator/components/parser/ZBParser";
import detailsFrom from "./orderDetail";
import orderSend from "./orderSend";
import orderVideoSend from "./orderVideoSend";
import { storeStaffListApi } from "@/api/storePoint";
import Cookies from "js-cookie";
import { isWriteOff } from "@/utils";
import { orderExcelApi } from "@/api/store";
import { checkPermi } from "@/utils/permission"; // 权限判断函数
import { string } from "_jszip@3.2.1@jszip/lib/support";
export default {
  name: "orderlistDetails",
  components: {
    cardsData,
    zbParser,
    detailsFrom,
    orderSend,
    orderVideoSend,
  },
  data() {
    return {
      zyform: {},
      orderId: "",
      refundVisible: false,
      refundData: {},
      dialogVisibleJI: false,
      tableDataLog: {
        data: [],
        total: 0,
      },
      tableFromLog: {
        page: 1,
        limit: 10,
        orderNo: 0,
      },
      LogLoading: false,
      isCreate: 1,
      editData: null,
      dialogVisible: false,
      tableData: {
        data: [],
        total: 0,
      },
      listLoading: true,
      tableFrom: {
        dateLimit: "",
        orderNo: "",
        page: 1,
        limit: 10,
        type: 0,
      },
      orderChartType: {},
      timeVal: [],
      fromList: this.$constants.fromList,
      fromType: [
        { value: "all", text: "全部" },
        { value: "info", text: "普通" },
        { value: "pintuan", text: "拼团" },
        { value: "bragin", text: "砍价" },
        { value: "miaosha", text: "秒杀" },
      ],
      selectionList: [],
      ids: "",
      orderids: "",
      cardLists: [],
      isWriteOff: isWriteOff(),
      proType: 0,
      active: false,
      remarksVisible: false,
      remarksInfo: {},
      zyVisible: false,
      zyData: {},
    };
  },
  mounted() {
    this.getList();
    this.getOrderStatusNum();
    // this.getOrderListData();
  },
  methods: {
    resetFormzy() {
      this.zyVisible = false;
    },
    // 争议
    zyClose() {
      this.zyVisible = false;
    },
    sendOrder(row) {
      this.zyform = {
        orderId: String(row.id),
        remark: "",
        status: "",
        type: "",
      };
      this.zyVisible = true;
    },
    sumbitZyVisible() {
      biddingOrderDispute(this.zyform).then(() => {
        this.$message.success("处理成功");
        this.zyVisible = false;
      });
    },

    seachList() {
      this.tableFrom.page = 1;
      this.getList();
      this.getOrderStatusNum();
    },

    pageChangeLog(page) {
      this.tableFromLog.page = page;
      this.onOrderLog();
    },
    handleSizeChangeLog(val) {
      this.tableFromLog.limit = val;
      this.onOrderLog();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 备注
    onOrderMark(row) {
      this.remarksVisible = true;
      this.remarksInfo = row;
      //   this.$prompt("订单备注", {
      //     confirmButtonText: "确定",
      //     cancelButtonText: "取消",
      //     inputErrorMessage: "请输入订单备注",
      //     inputType: "textarea",
      //     inputValue: row.remark,
      //     inputPlaceholder: "请输入订单备注",
      //     inputValidator: (value) => {
      //       if (!value) return "输入不能为空";
      //     },
      //   })
      //     .then(({ value }) => {
      //       orderMarkApi({ mark: value, orderNo: row.orderId }).then(() => {
      //         this.$message.success("操作成功");
      //         this.getList();
      //       });
      //     })
      //     .catch(() => {
      //       this.$message.info("取消输入");
      //     });
    },
    submitRemark(mark, remark, status, orderNo) {
      orderMarkApi({
        mark: mark,
        orderNo: orderNo,
        remark: remark,
        status: status,
      }).then(() => {
        this.$message.success("操作成功");
        this.getList();
      });
    },
    handleSelectionChange(val) {
      this.selectionList = val;
      const data = [];
      this.selectionList.map((item) => {
        data.push(item.orderId);
      });
      this.ids = data.join(",");
    },
    // 选择时间
    selectChange(tab) {
      this.timeVal = [];
      this.tableFrom.page = 1;
      this.getList();
      this.getOrderStatusNum();
      // this.getOrderListData();
    },
    // 具体日期
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.dateLimit = e ? this.timeVal.join(",") : "";
      this.tableFrom.page = 1;
      this.getList();
      this.getOrderStatusNum();
      // this.getOrderListData();
    },
    // 编辑
    edit(row) {
      this.orderId = row.orderId;
      this.editData = {
        orderId: row.orderId,
        totalPrice: row.totalPrice,
        totalPostage: row.totalPostage,
        payPrice: row.payPrice,
        payPostage: row.payPostage,
        gainIntegral: row.gainIntegral,
      };
      this.dialogVisible = true;
    },
    handlerSubmit(formValue) {
      let data = {
        orderNo: formValue.orderId,
        payPrice: formValue.payPrice,
      };
      updatePriceApi(data).then((data) => {
        this.$message.success("编辑数据成功");
        this.dialogVisible = false;
        this.getList();
      });
    },
    // 列表
    getList() {
      this.listLoading = true;
      biddingOrderListApi(this.tableFrom)
        .then((res) => {
          this.tableData.data = res.list || [];
          this.tableData.total = res.total;
          this.listLoading = false;
          this.checkedCities = this.$cache.local.has("order_stroge")
            ? this.$cache.local.getJSON("order_stroge")
            : this.checkedCities;
        })
        .catch(() => {
          this.listLoading = false;
        });
    },
    // 数据统计
    getOrderListData() {
      orderListDataApi({ dateLimit: this.tableFrom.dateLimit }).then((res) => {
        this.cardLists = [
          {
            name: "订单数量",
            count: res.count,
            color: "#1890FF",
            class: "one",
            icon: "icondingdan",
          },
          {
            name: "订单金额",
            count: res.amount,
            color: "#A277FF",
            class: "two",
            icon: "icondingdanjine",
          },
          {
            name: "微信支付金额",
            count: res.weChatAmount,
            color: "#EF9C20",
            class: "three",
            icon: "iconweixinzhifujine",
          },
          {
            name: "余额支付金额",
            count: res.yueAmount,
            color: "#1BBE6B",
            class: "four",
            icon: "iconyuezhifujine2",
          },
        ];
      });
    },
    // 获取各状态数量
    getOrderStatusNum() {
      orderStatusNumApi({
        dateLimit: this.tableFrom.dateLimit,
        type: this.tableFrom.type,
      }).then((res) => {
        this.orderChartType = res;
      });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList();
    },
    exports() {
      let data = {
        dateLimit: this.tableFrom.dateLimit,
        orderNo: this.tableFrom.orderNo,
        status: this.tableFrom.status,
        type: this.tableFrom.type,
      };
      orderExcelApi(data).then((res) => {
        window.open(res.fileName);
      });
    },
    //打印小票
    onOrderPrint(data) {
      orderPrint(data.orderId)
        .then((res) => {
          this.$modal.msgSuccess("打印成功");
        })
        .catch((error) => {
          this.$modal.msgError(error.message);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.el-table__body {
  width: 100%;
  table-layout: fixed !important;
}
.demo-table-expand {
  ::v-deeplabel {
    width: 83px !important;
  }
}
.refunding {
  span {
    display: block;
  }
}
.selWidth {
  width: 300px;
}
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
  font-size: 12px;
}
.el-icon-arrow-down {
  font-size: 12px;
}
.tabBox_tit {
  font-size: 12px !important;
  /*margin: 0 2px 0 10px;*/
  letter-spacing: 1px;
  /*padding: 5px 0;*/
  box-sizing: border-box;
}
.text_overflow {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 400px;
}
.pup_card {
  width: 200px;
  border-radius: 5px;
  padding: 5px;
  box-sizing: border-box;
  font-size: 12px;
  line-height: 16px;
}
.flex-column {
  display: flex;
  flex-direction: column;
}
.mt20 {
  margin-top: 20px;
}
.relative {
  position: relative;
}
</style>
